package com.mflex.WHSmartWhouseAd.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;


import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.adapter.FlexShelfOffAdapter;
import com.mflex.WHSmartWhouseAd.adapter.OutShelfDetailAdapter;
import com.mflex.WHSmartWhouseAd.model.TaskFlexModel;
import com.mflex.WHSmartWhouseAd.model.TransTaskInfo;
import com.mflex.WHSmartWhouseAd.utils.HideKeyBoardUtil;
import com.mflex.WHSmartWhouseAd.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.List;

public class FlexShelfOffActivity extends AppCompatActivity {
    EditText stvOperator;
    EditText stVehicleNO;
    ListView txtList;
    MiniLoadingDialog mMiniLoadingDialog;

    List<TaskFlexModel> TaskFlexlist;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_flex_shelf_off);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");

        stvOperator= findViewById(R.id.stv_Employee);
        stVehicleNO= findViewById(R.id.stvVehicleNO);
        txtList= findViewById(R.id.lvList);
        stvOperator.setFocusable(true);
        stvOperator.setFocusableInTouchMode(true);
        stvOperator.requestFocus();
        HideKeyBoardUtil.disableShowSoftInput(stvOperator);
        stvOperator.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){
                    HideKeyBoardUtil.hideKeyBoard(FlexShelfOffActivity.this);
                }
            }
        });

        txtList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(FlexShelfOffActivity.this, FlexShelfOffOrderActivity.class);
                intent.putExtra("SubTaskNo", TaskFlexlist.get(i).getSubTaskNo());
                intent.putExtra("Floor", TaskFlexlist.get(i).getFloor());
                intent.putExtra("SMTProduct", TaskFlexlist.get(i).getSMTProduct());
                intent.putExtra("FLEXProduct", TaskFlexlist.get(i).getFlexProduct());
                intent.putExtra("TaskFlexOrder", JsonUtil.toJson(TaskFlexlist.get(i).getOrderList()));
                intent.putExtra("userName", stvOperator.getText().toString());

                //第三层页面需要涨缩，计划数量
                intent.putExtra("PlanQty", TaskFlexlist.get(i).getPlanQty());
                intent.putExtra("ZoomScale", TaskFlexlist.get(i).getZoomScale());
                startActivity(intent);
            }
        });

        stVehicleNO.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    if(StringUtils.equals("",stvOperator.getText().toString()))  {
                        mMiniLoadingDialog.dismiss();
                        MediaPlayer mp = MediaPlayer.create(FlexShelfOffActivity.this, R.raw.error_voice);mp.start();
                        XToastUtils.success("请先扫描员工号", 1500);
                        stvOperator.requestFocus();
                        return false;
                    }
                    mMiniLoadingDialog.show();
                    XHttp.get("api/WHSmartWhousePDA/GetTaskList")
                            .params("CarrrierName", stVehicleNO.getText().toString())
                            .syncRequest(false)
                            .onMainThread(true)
                            .timeOut(120000)
                            .execute(new SimpleCallBack<List<TaskFlexModel>>() {
                                @Override
                                public void onSuccess(List<TaskFlexModel> response) {
                                    mMiniLoadingDialog.dismiss();
                                    TaskFlexlist=response;
                                    FlexShelfOffAdapter tfFlexShelfOffAdapter=new FlexShelfOffAdapter(FlexShelfOffActivity.this,TaskFlexlist);
                                    txtList.setAdapter(tfFlexShelfOffAdapter);
                                }

                                @Override
                                public void onError(ApiException e) {
                                    mMiniLoadingDialog.dismiss();
                                    stVehicleNO.setText("");
                                    stVehicleNO.requestFocus();
                                    MediaPlayer mp = MediaPlayer.create(FlexShelfOffActivity.this, R.raw.error_voice);mp.start();
                                    new MaterialDialog.Builder(view.getContext())
                                            .iconRes(R.drawable.icon_warning)
                                            .title(R.string.tip_warning)
                                            .content(e.getMessage())
                                            .positiveText(R.string.lab_submit)
                                            .show();
                                }
                            });
                }
                return false;
            }
        });

        stvOperator.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {
//                mMiniLoadingDialog.show();
//                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
//                    if (stvOperator.getText().toString() == "") {
//                        mMiniLoadingDialog.dismiss();
//                        MediaPlayer mp = MediaPlayer.create(FlexShelfOffActivity.this, R.raw.error_voice);mp.start();
//                        XToastUtils.success("请先扫描员工号", 1500);
//                        stvOperator.requestFocus();
//                        return false;
//                    }
//
//                    XHttp.get("api/WHSmartWhousePDA/CheckOperator")
//                            .params("OptFuncName", "柔板投料下架")
//                            .params("strOperator", stvOperator.getText().toString())
//                            .syncRequest(false)
//                            .onMainThread(true)
//                            .timeOut(120000)
//                            .execute(new SimpleCallBack<Boolean>() {
//                                @Override
//                                public void onSuccess(Boolean response) {
//                                    //stvOperator.setText(response);
//                                    mMiniLoadingDialog.dismiss();
//                                }
//
//                                @Override
//                                public void onError(ApiException e) {
//                                    mMiniLoadingDialog.dismiss();
//                                    stvOperator.setText("");
//                                    stvOperator.requestFocus();
//                                    MediaPlayer mp = MediaPlayer.create(FlexShelfOffActivity.this, R.raw.error_voice);mp.start();
//                                    new MaterialDialog.Builder(view.getContext())
//                                            .iconRes(R.drawable.icon_warning)
//                                            .title(R.string.tip_warning)
//                                            .content(e.getMessage())
//                                            .positiveText(R.string.lab_submit)
//                                            .show();
//                                }
//                            });
//                }
                return false;
            }
        });
    }

}